/* eslint-disable */
try {
  localStorage.setItem('isPrivateMode', '1');
  localStorage.removeItem('isPrivateMode');
  window.isPrivateMode = false;
} catch (e) {
  window.isPrivateMode = true;
}
function getCookie (c_name) {
  if (document.cookie.length > 0) {
    var c_start = document.cookie.indexOf(c_name + '=');
    if (c_start !== -1) {
      c_start = c_start + c_name.length + 1;
      var c_end = document.cookie.indexOf(';', c_start);
      if (c_end === -1) c_end = document.cookie.length;
      return decodeURIComponent(document.cookie.substring(c_start, c_end));
    }
  }
  return undefined;
}
function setCookie (c_name, value) {
  document.cookie = c_name + '=' + encodeURIComponent(value);
}

function clearAllCookie () {
  var keys = document.cookie.match(/[^ =;]+(?=\=)/g);
  if (keys) {
    for (var i = 0; i < keys.length; i++) {
      document.cookie = keys[i] + '=0;expires=' + new Date(0).toUTCString();
    }
  }
}
function delCookie (name) {
  var exp = new Date();
  exp.setTime(exp.getTime() - 1);
  var cval = getCookie(name);
  if (cval !== undefined && cval != null) {
    document.cookie = name + '=' + cval + ';expires=' + exp.toGMTString();
  }
}

function getCookieObj () {
  var obj = {},
    keys = document.cookie.match(/[^ =;]+(?=\=)/g);
  if (keys) {
    for (var i = 0; i < keys.length; i++) {
      obj[keys[i]] = getCookie(keys[i]);
    }
  }
  return obj;
}

var local = {};
local.update = function (data) {
  if (data._id !== undefined && data._id !== null) {
    local.set('id', data._id);
  } else {
    local.remove('id');
  }
};

local.get = function (key) {
  if (window.isPrivateMode) {
    return getCookie(key);
  } else {
    return localStorage.getItem(key);
  }
};

local.set = function (key, value) {
  if (window.isPrivateMode) {
    setCookie(key, value);
  } else {
    localStorage.setItem(key, value);
  }
};

local.clear = function () {
  if (window.isPrivateMode) {
    clearAllCookie();
  } else {
    localStorage.clear();
  }
};

local.raw = function () {
  if (window.isPrivateMode) {
    return getCookieObj();
  } else {
    return localStorage;
  }
};

local.remove = function (item) {
  if (window.isPrivateMode) {
    delCookie(item);
  } else {
    localStorage.removeItem(item);
  }
};

module.exports = local;
